<%--
  Created by IntelliJ IDEA.
  User: JesseChan
  Date: 2022/5/14
  Time: 19:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="java.util.Vector" %>
<%@ page import="com.biejiaogroup.librarymanager.entity.Book" %>
<%@ page import="com.biejiaogroup.librarymanager.controller.BookServlet" %>
<%@ page import="com.biejiaogroup.librarymanager.service.BookService" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="com.biejiaogroup.librarymanager.entity.User" %>
<%@ page import="com.biejiaogroup.librarymanager.service.UserService" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>个人中心</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
    <link href="css/font-awesome.css" rel="stylesheet">

    <link href="css/plugins/footable/footable.core.css" rel="stylesheet">
    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
</head>
<body class="gray-bg">
<%--顶部导航条--%>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="index.jsp">图书管理系统</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup"
            aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="topNavBar">
        <div class="navbar-nav">
            <a class="nav-link" href="mainPage.jsp">主页</a>
            <a class="nav-link active" href="user.jsp">个人中心</a>
        </div>
    </div>
    <input id="presentUserID" style="visibility: hidden" value="${user.userID}"></input>

    <%
        //登录按钮
        if (session.getAttribute("login") != null)
        {
            User user = (User) session.getAttribute("user");
            out.println("<td>欢迎" + user.getUserName() + "</td>");
            out.println("<div class=\"navbar-form navbar-right\">\n" +
                    "        <button class=\"btn btn-outline-danger my-2 my-sm-0\" type=\"button\" data-toggle=\"modal\" data-target=\"#exitModal\">退出登录</button>\n" +
                    "    </div>");
        }
        else
        {
            out.println("<div class=\"navbar-form navbar-right\">\n" +
                    "        <button class=\"btn btn-outline-success my-2 my-sm-0\" type=\"button\" data-toggle=\"modal\" data-target=\"#loginModal\">Login</button>\n" +
                    "    </div>");
        }
    %>
</nav>


<%--图书管理页面--%>
<div class="row">
    <div class="col-lg-12">
        <div class="ibox">
            <div class="ibox-content">

                <table class="footable table table-stripped toggle-arrow-tiny" data-page-size="15">
                    <thead>
                    <tr>
                        <th data-toggle="true">图书序号</th>
                        <th data-hide="phone">图书名称</th>
                        <th data-hide="all">描述</th>
                        <th data-hide="phone">图书作者</th>
                        <th data-hide="phone">图书状态</th>
                        <th class="text-right" data-sort-ignore="true">操作</th>
                    </tr>
                    </thead>

                    <tbody>
                    <%
                        Vector<Book> books = new Vector<Book>();
                        books = (Vector<Book>) session.getAttribute("books");
                        User user = (User) session.getAttribute("user");
                        if (books == null)
                        {
                            BookService bookService = new BookService();
                            books = bookService.getAll();
                        }

                        if (books == null)
                        {
                        }
                        else
                        {
                            for (Book book : books)
                            {
                                if (user != null && book.getBelongTo() == user.getUserID())
                                {
                                    out.println("<tr>");
                                    out.println("<td>" + book.getBookID() + "</td>");
                                    out.println("<td>" + book.getBookName() + "</td>");
                                    out.println("<td></td>");
                                    out.println("<td>" + book.getBookAuthor() + "</td>");

                                    if (book.getCanbeBorrowed() == 1)
                                        out.println("<td><span class=\"label label-primary\">可借阅</span></td>");
                                    else out.println("<td><span class=\"label label-danger\">已被借阅</span></td>");

                                    out.println("<td class=\"text-right\">");
                                    out.println("<div class=\"btn-group\">");
                                    out.println("<a data-toggle=\"modal\" class=\"btn btn-primary\" onclick=\"returnBook(this)\">归还</a>");
                                    out.println("</div>");
                                    out.println("</td>");

                                    out.println("<tr>");
                                }
                            }
                        }
                    %>
                    </tbody>
                    <tfoot>
                    <tr>
                        <td colspan="6">
                            <ul class="pagination float-right"></ul>
                        </td>
                    </tr>
                    </tfoot>
                </table>

            </div>
        </div>
    </div>

    <div id="exitModal" class="modal fade" aria-hidden="true" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">确定要退出吗？</h4>
                </div>
                <div class="modal-body">
                    <form class="m-t" role="form">
                        <button type="button" class="btn btn-sm btn-danger float-right m-t-n-xs"
                                id="btn_confirm_logout">退出
                        </button>
                    </form>
                </div>

            </div>
        </div>
    </div>
</div>
</body>

<!-- Mainly scripts -->
<script type="text/javascript" src="js/jquery-3.6.0.js"></script>
<script src="js/popper.min.js"></script>
<script type="text/javascript" src="js/bootstrap.bundle.js"></script>
<script src="js/plugins/metisMenu/jquery.metisMenu.js"></script>
<script src="js/plugins/slimscroll/jquery.slimscroll.min.js"></script>

<!-- Custom and plugin javascript -->
<script src="js/inspinia.js"></script>
<script src="js/plugins/pace/pace.min.js"></script>

<!-- FooTable -->
<script src="js/plugins/footable/footable.all.min.js"></script>

<script>
    $(document).ready(function () {

        $('.footable').footable();

    });

    function returnBook(obj) {
        var bookID = $(obj).parent().parent().parent().find('td').eq(0).text();
        var bookName = $(obj).parent().parent().parent().find('td').eq(1).text();
        var bookAuthor = $(obj).parent().parent().parent().find('td').eq(2).text();
        $.ajax({
            url: "returnBook",
            type: "post",
            data: {
                bookID: bookID,
            },
            success: function (data) {
                if (data == "success") {
                    alert("还书成功");
                    window.location.reload();
                } else {
                    alert("还书失败");
                }
            }
        });
    }

    //退出登录
    $("#btn_confirm_logout").click(function () {
        $.ajax({
            url: "logout",
            type: "post",
            success: function (data) {
                if (data == "success") {
                    window.location.href = "index.jsp";
                } else {
                    alert("退出失败");
                }
            }
        });
    });


</script>

</html>
